package cz.cvut.manatee.processManager;

import java.util.logging.*;
import java.io.IOException;
import java.io.File;


public class Logging {
	protected static Logger logger = null;
	protected static Handler filehandler = null;
	
	protected static String dir = "."; // slozka pro log soubory
	protected static String filename = "manatee-pm-%g.log"; // vzor pro log soubory
	protected static int maxFilesize = 1048576; // velikost LOG souboru v bytech
	protected static int maxLogFiles = 10; // maximalni pocet LOG souboru do kterych se bude zapisovat
	
	public static void create(String directory) throws IOException {
		if (logger == null) {
			setDir(directory); // overime cestu
			logger = Logger.getLogger("processmanager");
			Handler handler = null;
			filehandler = handler = new FileHandler(dir+"/"+filename, maxFilesize, maxLogFiles, false);
			handler.setFormatter(new SimpleFormatter());
			logger.addHandler(handler);
		}
    }
	
	public static Logger get() {
		//if  (logger == null) create();
		return logger;
	}
	
	
	public static void close() {
		if (filehandler != null) {
			filehandler.close();
			logger      = null;
			filehandler = null;
		}
	}
	
	protected static void setDir(String directory) throws IOException {
		File d = new File(directory);
		if (d.isDirectory() && d.canWrite()) {
			dir = d.getCanonicalPath();
		} else {
			throw new IOException("Zadana cesta \"" + directory + "\" neni adresar nebo do nej nelze zapisovat");
		}
	}
}
